import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class TransformCard extends StatefulWidget {
  const TransformCard({super.key});

  @override
  State<TransformCard> createState() => _TransformCardState();
}

class _TransformCardState extends State<TransformCard>
    with SingleTickerProviderStateMixin {
  double _rotateX = .0;
  double _rotateY = .0;
  late final AnimationController _animationController =
      AnimationController(vsync: this, duration: Duration(seconds: 1));
  int cardCount = 0;
  bool _isFront = true;

  @override
  void initState() {
    _animationController.repeat();
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('3D 变换Demo'),
        ),
        body: Column(
          children: [
            AnimatedBuilder(
              animation: _animationController,
              builder: (ctx, child) {
                // _rotateX += 1 * .01;
                _rotateX += 1 * -0.01;
                double _piYNum = _rotateY.abs() / pi / 2;
                double _piXNum = _rotateX.abs() / pi / 2;
                double _xNum = (_piXNum - (_piXNum.toInt()));
                double _yNum = (_piYNum - (_piYNum.toInt()));
                // _isOdd = _piNum.toInt() % 2 == 0 ? true : false;
                if ((_yNum > 0.25 && _yNum < 0.75) ||
                    (_xNum > 0.25 && _xNum < 0.75)) {
                  print('设置正面 ${_xNum},${_xNum < 0.25 || _yNum < 0.25}');
                  _isFront = false;
                } else {
                  print('设置反面 ${_xNum}');
                  _isFront = true;
                }
                return GestureDetector(
                  onPanDown: (details) {
                    _animationController.stop();
                  },
                  onPanEnd: (details) {
                    _animationController.repeat();
                  },
                  onPanUpdate: (details) {
                    setState(() {
                      _rotateX += details.delta.dy * 0.01;
                      _rotateY += details.delta.dx * -0.01;
                      // print("_rotateY: ${details.delta.dx}");
                      // print("_rotateY: ${_rotateY}");
                    });
                    double _piYNum = _rotateY.abs() / pi / 2;
                    double _piXNum = _rotateX.abs() / pi / 2;
                    double _xNum = (_piXNum - (_piXNum.toInt()));
                    double _yNum = (_piYNum - (_piYNum.toInt()));
                    // _isOdd = _piNum.toInt() % 2 == 0 ? true : false;
                    if ((_yNum > 0.25 && _yNum < 0.75) ||
                        (_xNum > 0.25 && _xNum < 0.75)) {
                      print('设置正面 ${_xNum},${_xNum < 0.25 || _yNum < 0.25}');
                      setState(() {
                        _isFront = false;
                      });
                    } else {
                      print('设置反面 ${_xNum}');

                      setState(() {
                        _isFront = true;
                      });
                    }
                  },
                  child: Transform(
                    transform: Matrix4.identity()
                      ..setEntry(3, 2, 0.001)
                      ..rotateX(_rotateX)
                      ..rotateY(_rotateY),
                    alignment: Alignment.center,
                    child: IndexedStack(
                      index: _isFront ? 0 : 1,
                      children: [
                        Container(
                          alignment: Alignment.center,
                          color: Colors.blue,
                          width: 300,
                          height: 300,
                          child: Text('3D 变换Demo'),
                        ),
                        Container(
                          alignment: Alignment.center,
                          color: Colors.red,
                          width: 300,
                          height: 300,
                          child: Text(
                            '我是背面',
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            )
          ],
        ));
  }
}
